import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import util.ShiroUtils;

/**
 * 登录，登出，验证身份
 */
public class Shiro1 {

    public static void main(String[] args) {

        //获取SecurityManager工厂，此处使用ini配置文件初始化SecurityManager
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro1/shiro.ini");
        //得到securityManager并绑定到securityUtils
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        //得到Subject创建用户名和密码的身份验证token（即用户身份/凭证）
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("dayu", "123");

        ShiroUtils.userLogin(subject, token);

        PrincipalCollection principals = subject.getPrincipals();
        System.out.println("成功认证：" + principals.asList().size() + " 人，用户名：" + principals);

        System.out.println(subject.isAuthenticated());
    }
}
